<?php
/*
 * Created on Oct 14, 2008
 *
 * Add the character.
 */
 
session_start();

if ( !isset($_SESSION['user']) ) {
	exit;
}
 
require_once ("../server/dbconnect.php");

if (!isset($_FILES['userfile'])) {
	die ("You need to upload a file");
}

if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {
 		
	$maxsize = "65536";
	
    // check the file is less than the maximum file size
    if($_FILES['userfile']['size'] < $maxsize) {
    	    	
        // prepare the image for insertion
        $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
 
        // get the image info..
        $size = getimagesize($_FILES['userfile']['tmp_name']);
 
        // put the image in the db...
        // database connection
        $db = dbconnect();
 		
 		$text = $_POST['text'];
 		
        // the image insert sql query
        $sql = "INSERT INTO images".
                " ( id, type, image, text )" .
                " VALUES" .
                " ('', '{$size['mime']}', '{$imgData}', '$text')";
                
        // insert the image
        $query = $db->query($sql);
        if (DB::isError($query)) {
			$db->rollback();   	
        	die ("Unable to upload image: " . $query->getMessage());
        }
        
        $name = $_POST['character'];
        $series = $_POST['title'];
        $image = mysql_insert_id();
        $description = $_POST['description'];
        
        // the character insert sql query
        $sql = "INSERT INTO characters" .
        		" ( name, title, image, description )" . 
        		" VALUES" .
        		" ('$name', $series, $image, '$description' )";
        		
		// insert the character 
       	$query = $db->query($sql);
        if (DB::isError($query)) {
			$db->rollback();        	
        	die ("Unable to add character: " . $query->getMessage());
        } 
        
        $db->commit();
        
        $db->disconnect();
      	
      	header('Location: animesetup.php');  
    } else {
         // if the file is not less than the maximum allowed, print an error
         echo
          '<div>File exceeds the Maximum File limit</div>
          <div>Maximum File limit is '.$maxsize.'</div>
          <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].' bytes</div>
          <hr />';
    }
} else {
	echo $_FILES['userfile']['error'][0]; 
}
?>
